package MIU.TwistedLabirynth.menu;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import MIU.TwistedLabirynth.pac.GameLogicController;

public class SettingsMenu {

	public static final int LEVEL_EASY = 0;
	public static final int LEVEL_MEDIUM = 1;
	public static final int LEVEL_HARD = 2;

	private BitmapTextureAtlas mMusicTexture;
	private BitmapTextureAtlas difficultyLevelTexture;

	private TextureRegion mMusicTextureRegion;
	private TextureRegion difficultyLevelTextureRegion;

	private final GameLogicController gameLogicController;

	private boolean musicEnabled = true;
	private int difficultyLevel = 0;

	public SettingsMenu(GameLogicController gameLogicController) {
		this.gameLogicController = gameLogicController;
	}

	public void loadResources() {

		this.mMusicTexture = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mMusicTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mMusicTexture,
				gameLogicController,
				"menu_black_button_music_on.png", 0, 0);
		this.difficultyLevelTexture = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.difficultyLevelTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				this.difficultyLevelTexture, gameLogicController, "level_easy.png", 0, 0);

		gameLogicController.getEngine().getTextureManager()
				.loadTextures(this.mMusicTexture, this.difficultyLevelTexture);

	}

	public Scene createSettingSubMenu() {
		Scene scene = new Scene();
		final Sprite musicButton = new Sprite(100, 50, 250, 70, this.mMusicTextureRegion) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (gameLogicController.checkTouchTime()) {
					SettingsMenu.this.mMusicTexture.clearTextureAtlasSources();
					SettingsMenu.this.musicEnabled = !SettingsMenu.this.musicEnabled;
					BitmapTextureAtlasTextureRegionFactory.createFromAsset(SettingsMenu.this.mMusicTexture,
							gameLogicController, SettingsMenu.this.musicEnabled ? "menu_black_button_music_on.png"
									: "menu_black_button_music_off.png", 0, 0);
				}
				return true;
			}

		};
		BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMusicTexture, gameLogicController,
				SettingsMenu.this.musicEnabled ? "menu_black_button_music_on.png" : "menu_black_button_music_off.png",
				0, 0);
		scene.registerTouchArea(musicButton);
		scene.attachChild(musicButton);

		final Sprite difficultyLevelButton = new Sprite(100, 130, 250, 70, this.difficultyLevelTextureRegion) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (gameLogicController.checkTouchTime()) {
					SettingsMenu.this.difficultyLevelTexture.clearTextureAtlasSources();
					changeDifficultyLevel();
					BitmapTextureAtlasTextureRegionFactory.createFromAsset(SettingsMenu.this.difficultyLevelTexture,
							gameLogicController, getDifficultyLevelIconName(), 0, 0);
				}
				return true;
			}
		};
		BitmapTextureAtlasTextureRegionFactory.createFromAsset(difficultyLevelTexture,
				gameLogicController, getDifficultyLevelIconName(), 0, 0);
		scene.registerTouchArea(difficultyLevelButton);
		scene.attachChild(difficultyLevelButton);

		return scene;
	}

	private void changeDifficultyLevel() {
		difficultyLevel++;
		if (difficultyLevel > LEVEL_HARD) {
			difficultyLevel = LEVEL_EASY;
		}
	}

	private String getDifficultyLevelIconName() {
		switch (difficultyLevel) {
		case 0:
			return "level_easy.png";
		case 1:
			return "level_medium.png";
		case 2:
			return "level_hard.png";
		default:
			return "level_easy.png";
		}

	}

	public boolean isMusicEnabled() {
		return musicEnabled;
	}

	public int getDifficultyLevel() {
		return difficultyLevel;
	}

	public void setMusicEnabled(boolean musicEnabled) {
		this.musicEnabled = musicEnabled;
	}

	public void setDifficultyLevel(int difficultyLevel) {
		this.difficultyLevel = difficultyLevel;
	}
}
